package com.zhixue.content.api;

import com.zhixue.content.model.dto.CoursePreviewDto;
import com.zhixue.content.model.po.CoursePublish;
import com.zhixue.content.service.CoursePublishService;
import com.zhixue.content.util.SecurityUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Dragon
 * @description CoursePublishController
 * @date 2023/10/8 15:44
 */
@Controller
public class CoursePublishController {

    @Autowired
    CoursePublishService coursePublishService;

    /**
     * 课程预览
     * @param courseId
     * @return
     */
    @GetMapping("/coursepreview/{courseId}")
    public ModelAndView preview(@PathVariable("courseId") Long courseId){
        CoursePreviewDto coursePreviewInfo = coursePublishService.getCoursePreviewInfo(courseId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("course_template");
        modelAndView.addObject("model", coursePreviewInfo);
        return modelAndView;
    }


    /**
     * 提交课程审核
     *
     * @param courseId 课程id
     */
    @ResponseBody
    @PostMapping("/courseaudit/commit/{courseId}")
    public void commitAudit(@PathVariable Long courseId) {
        //SecurityUtil.XcUser user = SecurityUtil.getUser();
        //Long companyId = 1232141425L;
        SecurityUtil.ZxUser user = SecurityUtil.getUser();
        Long companyId = null;
        if (StringUtils.isNotEmpty(user.getCompanyId())) {
            companyId = Long.parseLong(user.getCompanyId());
        }
        coursePublishService.commitAudit(companyId, courseId);
    }



    @ApiOperation("课程发布")
    @ResponseBody
    @PostMapping("/coursepublish/{courseId}")
    public void coursePublish(@PathVariable Long courseId) {
        //Long companyId = 1232141425L;
        SecurityUtil.ZxUser user = SecurityUtil.getUser();
        Long companyId = null;
        if (StringUtils.isNotEmpty(user.getCompanyId())) {
            companyId = Long.parseLong(user.getCompanyId());
        }
        coursePublishService.publishCourse(companyId ,courseId);
    }

    @ApiOperation("查询课程发布信息")
    @ResponseBody
    @GetMapping("/r/coursepublish/{courseId}")// 将/r打头的都作为服务内部调用的接口，白名单中配置
    public CoursePublish getCoursePublish(@PathVariable("courseId") Long courseId) {
       CoursePublish coursePublish =coursePublishService.getCoursePublish(courseId);
       return coursePublish;
    }


    @ApiOperation("获取课程发布信息")
    @ResponseBody
    @GetMapping("/course/whole/{courseId}")
    public CoursePreviewDto getCoursePreviewDto(@PathVariable("courseId") Long courseId) {
        return coursePublishService.getCoursePreviewInfo(courseId);
    }
}
